<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>fetch</title>
</head>
<body>

  <script>
    const url = 'https://www.imooc.com/api/http/search/suggest?words=js';

    const fd = new FormData();
    fd.append('age',5000);

    fetch(url,{
      method:"post",
      // 请求体携带数据
      /*body:'username=swk',
      headers:{'Content-Type':'application/x-www-form-urlencoded'}*/
      /*body:JSON.stringify({username:'swk'}),
      headers:{'Content-Type':'application/json'}*/
      body:fd,
      mode:'cors',    // 跨域
      // credentials:''    // cookie
    }).then(response =>{
      console.log(response);

      // 判断数据是否可用
      if (response.ok){
        // return response.json();    // JSON 格式数据
        return response.text();    // 字符串格式
      }else {
        // 抛出异常的状态码
        throw new Error(`http code 异常 ${response.status}`);
      }
    }).then(data =>{
      console.log(data);
    }).catch(err =>{
      console.log(err);
    })
  </script>
</body>
</html>